package com.wuhan.template.module.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wuhan.template.base.BaseApplication;
import com.wuhan.template.module.find.webview.WebViewActivity;
import com.wuhan.template.module.home.entity.JiaoYuActivityEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/12 0012.
 */
public class JiaoYuActivityPageAdapter extends PagerAdapter {
    private Context context;
    private List<JiaoYuActivityEntity.SlideBean> slides;
    private ImageLoader loader;
    private DisplayImageOptions options;
    private List<ImageView> imageViews=new ArrayList<>();

    public JiaoYuActivityPageAdapter(Context context, List<JiaoYuActivityEntity.SlideBean> slides) {
        this.context = context;
        this.slides = slides;
        loader = ((BaseApplication)(context.getApplicationContext())).getImageLoader();
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();
        for (int i = 0; i < slides.size(); i++) {
            ImageView imageView = new ImageView(context);
            imageViews.add(imageView);
        }
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public Object instantiateItem(ViewGroup container,  int position) {
        position %= slides.size();
        if(position<0){
            position = slides.size()+position;
        }
        JiaoYuActivityEntity.SlideBean.IndexpicBean indexpic = slides.get(position).getIndexpic();
       String imageUrl=  indexpic.getHost()+indexpic.getDir()+indexpic.getFilepath()+indexpic.getFilename();

        ImageView imageView = imageViews.get(position);
        loader.displayImage(imageUrl, imageView, options);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

        ViewParent parent = imageView.getParent();
        if(parent!=null){
            ViewGroup group = (ViewGroup) parent;
            group.removeView(imageView);
        }

        container.addView(imageView);

        final int p = position;
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("url",slides.get(p).getContent_url());
                intent.putExtra("title",slides.get(p).getColumn_name());
                context.startActivity(intent);
            }
        });
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}
